<?php

/**
 * @see Zend_Auth_Adapter_Interface
 */
require_once 'Zend/Auth/Adapter/Interface.php';

/**
 * @see Zend_Db_Adapter_Abstract
 */
require_once 'Zend/Db/Adapter/Abstract.php';

/**
 * @see Zend_Auth_Result
 */
require_once 'Zend/Auth/Result.php';

/**
 * @category   Application
 * @package    User
 * @subpackage User_Plugin
 */
class User_Plugin_Auth_DbTable implements Zend_Auth_Adapter_Interface
{

	CONST LOGIN_MODE_USER_OR_EMAIL = 1;
	CONST LOGIN_MODE_USER = 2;
	CONST LOGIN_MODE_EMAIL = 3;

	/**
	 * @property int
	 */
	protected $_loginMode = 1;

	/**
	 * @property string
	 */
	protected $_identity = '';

	/**
	 * @property string
	 */
	protected $_crendential = '';

	/**
	 * set identity string
	 * @param string $identity
	 * @return User_Plugin_Auth_Email
	 */
	public function setIdentity($identity)
	{
		$this -> _identity = $identity;
		return $this;
	}

	/**
	 * set credential string
	 * @param string $credential
	 * @return User_Plugin_Auth_Email
	 */
	public function setCredential($credential)
	{
		$this -> _crendential = $credential;
		return $this;
	}

	/**
	 * get identity string
	 * @return string
	 */
	public function getIdentity()
	{
		return $this -> _identity;
	}

	/**
	 * get credetial string
	 * @return string
	 */
	public function getCredential()
	{
		return $this -> _crendential;
	}

	/**
	 * get login string
	 * @return int
	 */
	public function getLoginMode()
	{
		return $this -> _loginMode;
	}

	/**
	 * set login mode
	 * @param int $mode
	 * @return User_Plugin_Auth_DbTable
	 */
	public function setLoginMode($mode)
	{
		$this -> _loginMode = (int)$mode;
		return $this;
	}

	/**
	 * authenticate() - defined by Zend_Auth_Adapter_Interface.  This method is
	 * called to
	 * attempt an authentication.  Previous to this call, this adapter would have
	 * already
	 * been configured with all necessary information to successfully connect to a
	 * database
	 * table and attempt to find a record matching the provided identity.
	 *
	 * @throws Zend_Auth_Adapter_Exception if answering the authentication query is
	 * impossible
	 * @return Zend_Auth_Result
	 */
	public function authenticate()
	{
		$table = new User_Model_DbTable_Users;
		$select = $table -> select();

		switch($this->_loginMode) {
			case 3 :
				$select -> where('email=?', $this -> getIdentity());
			case 2 :
				$select -> where('username=?', $this -> getIdentity());
				break;
			case 1 :
			default :
				$select -> orWhere('email=?', $this -> getIdentity()) -> orWhere('username=?', $this -> getIdentity());
		}

		// get user by identity
		$user = $table -> fetchRow($select);

		echo $select;

		if (!is_object($user))
		{
			return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, 0, array());
		}else
		if (!$user -> isValidPassword($this -> getCredential()))
		{
			return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_CREDENTIAL_INVALID, 0, array());
		}
		else
		{
			switch(strtolower($user->status)) {
				case 'active' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::SUCCESS, $user -> getId(), array());
				case 'blocked' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_USER_BLOCKED, 0, array());
				case 'banned' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_USER_BANNED, 0, array());
				case 'expired' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_USER_EXPIRED, 0, array());
				case 'unapproved' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_USER_UNAPPROVED, 0, array());
				case 'inactive' :
					return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_USER_INACTIVE, 0, array());
			}
		}
		return new User_Plugin_Auth_Result(User_Plugin_Auth_Result::FAILURE_UNCATEGORIZED, 0, array());
	}

}
